package org.itboys.ace.web.lasted;

import org.itboys.admin.entity.lasted.SystemEmployee;
import org.itboys.admin.service.lasted.SystemEmployeeService;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.ErrorException;
import org.itboys.core.entity.mongo.SundayEmployee;
import org.itboys.core.service.mongo.web.SundayEmployeeService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author：贾琦磊
 * @Date：2018/9/6
 * @Description：
 */
@Controller
@RequestMapping("/ace/system/employee")
public class AceSystemEmployeeController extends BaseController{
    @Autowired
    private SystemEmployeeService employeeService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpServletResponse response) {
        return "/ace/sys/employee/index";
    }

    @RequestMapping("/select")
    public void select(HttpServletResponse response, HttpServletRequest request) throws Exception {
        PageResult<SystemEmployee> result = employeeService.select(request);
        JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
    }

    @RequestMapping("/input")
    public String input(HttpServletRequest request, HttpServletResponse response
            , @RequestParam("id") Long id, Model model) {
        model.addAttribute("employee", employeeService.getById(id));
        return "/ace/sys/employee/input";
    }

    @RequestMapping("/save")
    public void save(HttpServletRequest request, HttpServletResponse response
            , @RequestParam("id") Long id
            , @RequestParam("sex") Integer sex
            , @RequestParam("name") String name
            , @RequestParam("mobile") String mobile) throws Exception {
        SystemEmployee employee = new SystemEmployee(null, name, mobile, sex, null);
        employeeService.save(id, employee);
        ResponseMessage.success();
    }

    /**单选*/
    @RequestMapping("choose")
    public String choose(HttpServletRequest request,
                         @RequestParam(value = "isSingle", required = true) Integer isSingle,//是否单选
                         @RequestParam(value = "callBack", required = false) String callBack,//回调参数
                         @RequestParam(value = "param", required = false) String param,//查询参数
                         Model model) {
        model.addAttribute("param", param);
        model.addAttribute("callBack", callBack);
        if (isSingle == null) {
            return "/ace/sys/employee/choose/choose_erro";
        }
        return "/ace/sys/employee/choose/choose_single";
    }

    @RequestMapping("delete")
    public void delete(HttpServletRequest request,HttpServletResponse response
    ,@RequestParam("id")Long id){
        try{
            employeeService.delete(id);
            ResponseMessage.success();
        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg=e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/ace/system/employee/delete------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, msg);
        }
    }
}
